import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class SharedPreferencesPage extends StatefulWidget {
  const SharedPreferencesPage({super.key});

  @override
  State<SharedPreferencesPage> createState() => _SharedPreferencesPageState();
}

class _SharedPreferencesPageState extends State<SharedPreferencesPage> {

  Future<void> _save() async {
    SharedPreferences sp = await  SharedPreferences.getInstance();
    sp.setString("name", "张三");
  }

  _get() async {
    SharedPreferences sp = await  SharedPreferences.getInstance();
    var name = sp.getString("name");
    print(name);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Flutter"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(onPressed: _save, child: const Text("储存数据")),
            ElevatedButton(onPressed: _get, child: const Text("获取数据")),
          ],
        ),
      ),
    );
  }
}
